<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆ID" prop="bicycleId">
        <el-input v-model="queryParams.bicycleId" placeholder="请输入车辆ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入工单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:order:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:order:edit']">修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:order:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" @click="handleAudit"
          v-hasPermi="['system:order:audit']">审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="audit" plain size="mini" :disabled="multiple" @click="UnhandleAudit"
          v-hasPermi="['system:order:unaudit']">反审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:order:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bicycleList" @selection-change="handleSelectionChange" ref="table">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed="right" width="100" align="center">
        <template slot="header">
          <i class="el-icon-setting" style="font-size: 22px; cursor: pointer" @click="showColumnOption"></i>
        </template>
      </el-table-column>
      <el-table-column label="车辆ID" align="center" prop="bicycleId" />
      <el-table-column label="工单号" align="center" prop="orderId" />
      <el-table-column label="控制器号" align="center" prop="controllerId" />
      <el-table-column v-for="(item, index) in fittings" :key="index" :label="item.fittingName" align="center"
        :prop="item.fittingFabienName" v-if="item.delFlag" />
      <el-table-column label="审核状态" align="center" prop="orderFlag">
        <template #default="{ row }">
          <el-tag v-if="row.orderFlag == 1" type="success">已审核</el-tag>
          <el-tag v-if="row.orderFlag == 2" type="danger">未审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bicycle:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:bicycle:remove']">删除</el-button> -->
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <transition name="fade">
      <div class="columnOption" v-show="isShowColumn">
        <div class="content">
          <div class="head">选择显示字段</div>
          <div class="body">
            <el-checkbox v-for="(item, index) in fittings" :key="index" v-model="item.delFlag">{{ item.fittingName
            }}</el-checkbox>
          </div>
          <div class="footer">
            <el-button size="small" type="primary" plain @click="saveColumn">保存列配置</el-button>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { listBicycleorder, getBicycle, delBicycle, addBicycleorder, updateBicycle, updateAudit, updateUnAudit, selectFittings } from "@/api/storage/ordercollection/order";
import loginVue from '../../login.vue';
// import Gather from "./components/Gather.vue";

export default {
  // components: { Gather },
  name: "Bicycle",
  // comments: {
  //   Gather
  // },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //筛选显示
      isShowColumn: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ControlAcquisition表格数据
      bicycleList: [],
      fittings: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bicycleId: null,
        orderId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      checkList: {},
      showColumn: {
        date: true,
        name: true,
        provinces: true,
        city: true,
        adreess: true,
        zipCode: true,
      },
    };
  },
  created() {
    selectFittings().then(res => {
      this.fittings = res.data
      this.fittings.forEach(item => {
        if (item.delFlag == 0) {
          item.delFlag = true
        } else {
          item.delFlag = false
        }
      })
    })
  },
  mounted() {
    this.getList();
  },
  methods: {
    showColumnOption() {
      this.isShowColumn = true;
    },
    saveColumn() {
      this.isShowColumn = false;
    },
    /** 查询ControlAcquisition列表 */
    getList() {
      this.loading = true;
      listBicycleorder(this.queryParams).then(response => {
        this.bicycleList = response.rows;
        this.bicycleList.forEach(item => {
          this.fittings.forEach(it => {
            const fittingKeyName = it.fittingFabienName;
            if (item.map && item.map[fittingKeyName] && item.map[fittingKeyName] !== "") {
              item[fittingKeyName] = item.map[fittingKeyName]
            }
          })
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bicycleId: null,
        controllerId: null,
        frameId: null,
        motorId: null,
        batteryId: null,
        meterId: null,
        grossWeight: null,
        net: null,
        orderId: null,
        audit: null,
        controllerFlag: null,
        orderFlag: null,
        packagingFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bicycleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加四码采集";
      this.$router.push({
        path: '/storage/controller/controller-inster/index/' + '',
        query: { res: "2" }
      }
      )
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bicycleId = row.bicycleId || this.ids
      getBicycle(bicycleId).then(response => {
        if (response.data.orderFlag == 1) {
          this.$message.error("已审核无法修改")
        } else {
          this.$router.push({
            path: '/storage/controller/controller-inster/index/' + bicycleId,
            query: { res: "2" }
          })

        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bicycleId != null) {
            updateBicycle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBicycle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bicycleIds = row.bicycleId || this.ids;
      this.$modal.confirm('是否确认删除ControlAcquisition编号为"' + bicycleIds + '"的数据项？').then(function () {
        return delBicycle(bicycleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bicycle/export', {
        ...this.queryParams
      }, `bicycle_${new Date().getTime()}.xlsx`)
    },
    /** 审核操作 */
    handleAudit(row) {
      const bicycleIds = row.ticketsId || this.ids;
      updateAudit(bicycleIds);
      this.getList();
      this.$modal.msgSuccess("审核成功");

    },
    UnhandleAudit(row) {
      const bicycleIds = row.ticketsId || this.ids;
      updateUnAudit(bicycleIds);
      this.getList();
      this.$modal.msgSuccess("反审核成功");

    }
  }

};
</script>
<style scoped>
.columnOption {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: row-reverse;
}

.columnOption .content {
  width: 100px;
  height: 100%;
  background-color: rgb(203, 223, 198);
}

.columnOption .content .head {
  width: 100%;
  height: 44px;
  border-bottom: 1px solid #000;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}

.columnOption .content .body {
  width: 100%;
  height: calc(100% - 88px);
  box-sizing: border-box;
  padding-top: 10px;
  overflow-y: auto;
}

.columnOption .content .body .items {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.columnOption .content .body .items .el-checkbox {
  width: 100%;
  height: 28px;
  line-height: 28px;
  margin-bottom: 14px;
  display: inline-block;
  font-family: PingFang SC;
  font-style: normal;
  font-weight: normal;
  font-size: 14px;
  color: #000;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
  padding-left: 14px;
}

.columnOption .content .body .items .el-checkbox:hover {
  background-color: #f5f7fa;
}

.columnOption .content .footer {
  width: 100%;
  height: 44px;
  border-top: 1px solid #000;
  display: flex;
  justify-content: center;
  align-items: center;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
